package cn.stronglink.asset.module.basicdata.tRoom.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.TRoom;
import cn.stronglink.asset.module.basicdata.tRoom.mapper.TRoomMapper;
import cn.stronglink.asset.module.basicdata.tRoom.vo.RoomTreeNodeVo;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class TRoomService {

	@Autowired
	private TRoomMapper tRoomMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<TRoom> queryTRoom(Page<TRoom> page,Map<String, Object>  params) {
		page.setRecords(tRoomMapper.queryTRoom(page,params));
		return page;	
	}
	
	public void removeTRoom(Long id){
		tRoomMapper.removeTRoom(id);	
	}	

	public void insertTRoom(TRoom tRoom) {
		if (tRoom.getId() == null) {
			tRoom.setId(IdWorker.getId());
			tRoom.setType(1);
			tRoom.setCreateTime(new Date());
			tRoom.setCreateBy(WebUtil.getCurrentUser());
			tRoomMapper.insertTRoom(tRoom);		
		}else{
			tRoom.setUpdateBy(WebUtil.getCurrentUser());
			tRoom.setUpdateTime(new Date());
			tRoomMapper.updateTRoom(tRoom);
		}	
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<TRoom> queryRoomList() {
		return tRoomMapper.queryRoomList();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<TRoom> queryAllRoomList() {
		return tRoomMapper.queryAllRoomList();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int qryRoomCount() {
		return tRoomMapper.qryRoomCount();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Integer getRoomType(Long roomId) {
		return tRoomMapper.getRoomType(roomId);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<RoomTreeNodeVo> queryRoomListTree() {   
		List<RoomTreeNodeVo> voList = tRoomMapper.queryRoomListTree();
		if(voList!=null&&voList.size()>0){
			for(RoomTreeNodeVo vo:voList){
				List<RoomTreeNodeVo> rackList =tRoomMapper.queryRackList(vo.getId());
				vo.setRackList(rackList);
			}
			
		}
		return voList;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<HashMap<String, Object>> queryRoomU(Map<String, Object> params) {
		List<HashMap<String, Object>> mapList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> mapU =new HashMap<String, Object>();
		if(!"".equals(params.get("roomId"))&&params.get("roomId")!=null){
			mapU =tRoomMapper.queryRoomU(Long.parseLong(params.get("roomId").toString()));
		}else{
			List<TRoom> list =tRoomMapper.queryRoomList();
			if(list!=null&&list.size()>0){
				mapU =tRoomMapper.queryRoomU(list.get(0).getId());
			}
		}
		if(mapU!=null){
			int Utotal =Integer.parseInt(mapU.get("Utotal").toString());
			int userd = Integer.parseInt(mapU.get("value").toString());
			HashMap<String, Object> mapUnused =new HashMap<String, Object>();
			mapUnused.put("name", "未用");
			mapUnused.put("value", Utotal-userd);
			mapList.add(mapU);
			mapList.add(mapUnused);
		}
		return mapList;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<TRoom> queryRoomListByParams(Map<String, Object> params) {
		return tRoomMapper.queryRoomListByParams(params);
	}	

}
